﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Estimate.Common.Repository;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Estimate;

namespace Estimate.Common.DBHealth
{
    /// <summary>
    /// 检测预报表的问题
    /// </summary>
    public class CityForeHealth
    {
       
        string ForeDataType;
        ForeSemiType SemiType = ForeSemiType.Auto;

        public CityForeHealth(string foreDataType)
        {
            ForeDataType = foreDataType;
        }

        public DataTable CheckHealth(DateTime beginDate, DateTime endDate,ForeFactorType foreType,ref string msg)
        {

            DataTable CheckResult = new DataTable();
            CheckResult.Columns.Add("msg");
            CheckResult.Columns.Add("table");
            CheckResult.Columns.Add("date");
            CheckResult.Columns.Add("factortype");
            CheckResult.Columns.Add("semitype");
            CheckResult.Columns.Add("sendpoint");
            CheckResult.Columns.Add("span");
            CheckResult.DefaultView.Sort = "msg asc,date asc,span asc";

            ForeRepository repository=null;

            if (ForeDataType == "city")
            {
                repository = ForeRepositoryFactory.NormalCityForeRepository(ConfigurationManager.AppSettings["ConnectionString"]);
                msg = "城镇报数据";
            }
            else if (ForeDataType == "site")
            {
                repository = ForeRepositoryFactory.StSiteForeRepository(ConfigurationManager.AppSettings["ConnectionString"]);
                msg = "指导报数据";
            }
            else if (ForeDataType == "zyt")
            {
                repository = ForeRepositoryFactory.ZytCityForeRepository(ConfigurationManager.AppSettings["ConnectionString"]);
                msg = "国家局报数据";
            }

            List<string> CheckKeys =null;
            //List<ForeSpanType> Spans=new List<ForeSpanType>();
            //List<ForeSendpointType> Sendpoints = new List<ForeSendpointType>();
            if (foreType == ForeFactorType.wth)
              CheckKeys=  CheckForeDataKeys.GetWthCheckKeys(ForeDataType);
            else if (foreType == ForeFactorType.maxt || foreType == ForeFactorType.mint)
              CheckKeys=  CheckForeDataKeys.GetTempCheckKeys(ForeDataType);
            //SendpointToClock parser = new SendpointToClock();
            //ForeClockType[] clocks = parser.GetClockArrayWithSendPointArray(Sendpoints.ToArray());
            //按日期分类
            Dictionary<DateTime, Dictionary<string, ForeEntity>> Entities = new Dictionary<DateTime, Dictionary<string, ForeEntity>>();
            DateTime cur = beginDate;
             while (cur <= endDate)
             {
                 Entities.Add(cur, new Dictionary<string, ForeEntity>());
                 cur = cur.AddDays(1);
             }

            ForeEntity[] entities = repository.InquerySemiWithDateRange(beginDate, endDate, foreType, SemiType);
            foreach (ForeEntity entity in entities)
                Entities[entity.Date].Add(entity.Span.ToString() + "-" + entity.Sendpoint.ToString(), entity);

            foreach (KeyValuePair<DateTime, Dictionary<string, ForeEntity>> pair in Entities)
            {
                if (pair.Value.Count != CheckKeys.Count)
                {
                    foreach (string key in CheckKeys)
                    {
                        if (!pair.Value.ContainsKey(key))
                        {
                            string[] strs = key.Split('-');
                            CheckResult.Rows.Add(new object[] { "缺少记录", repository.TableName, pair.Key.ToString("yyyy-MM-dd"), foreType, SemiType, strs[1], strs[0] });
                        }
                    }
                }


                               
            }
        }

        public override ForeSendpointType[] GetSendpointArray()
        {
            return new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H10, ForeSendpointType.H16 };
        }

        public override RealStationCompareType GetCompareType(ForeSemiType semitype)
        {
            if (semitype == ForeSemiType.Auto)
                return RealStationCompareType.Normal_Auto;
            else
                return RealStationCompareType.Normal_Manual;
        }


    }
}
